<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Auth;

class ShopBalanceLog extends Model
{
    const UPDATED_AT = null; // 禁用 updated_at

    const TYPE_ORDER = 'order';
    const TYPE_RECHARGE= 'recharge';
    const TYPE_ADJUSTMENT = 'adjustment';
    const TYPE_WITHDRAW = 'withdraw';

    /**
     * 店铺。
     */
    public function shop()
    {
        return $this->belongsTo('App\Models\Shop', 'shop_id');
    }

    /**
     * 限制店铺。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOwnerShop($query)
    {
        return $query->where('shop_id', Auth::guard('shop')->user()->id);
    }

    /**
     * 账号变动类型
     *
     * @param String $type
     * @return String
     */
    public static function getTypes($type) {
        $types = [
            self::TYPE_ORDER      => '订单',
            self::TYPE_RECHARGE   => '充值',
            self::TYPE_ADJUSTMENT => '调整',
            self::TYPE_WITHDRAW   => '提现',
        ];

        if (!empty($type)){
            if (isset($types[$type])){
                return $types[$type];
            }else{
                return '';
            }
        }

        return $types;
    }

    /**
     * 获取用户的名字。
     *
     * @return string
     */
    public function getTypeLabelAttribute()
    {
        return $this->getTypes($this->attributes['type']);
    }

}
